/* 

Copyright (c) 2008 Joseph Abenhaim.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

 */

package com.uimind.components.fsdisplay
{
	import flash.display.StageDisplayState;
	import flash.events.FullScreenEvent;
	
	import mx.containers.Canvas;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	
	[DefaultProperty("content")]
	
	public class FSDisplay extends Canvas
	{
		// Private Elements
		private var _originalHeight:Number;
        private var _originalWidth:Number;
        private var _componentInst:UIComponent;
        private var _dispState:String = "";
        private var _rawPlayer:Canvas;
		private var _content:Array;
		private var _contentChanged:Boolean = false;
		
		public function FSDisplay()
		{
			super();
			addEventListener(FlexEvent.CREATION_COMPLETE, initComponent);
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			// Creates the default container to hold the assets that will go full screen & adds it to the stage.
			_rawPlayer = new Canvas();
			_rawPlayer.percentHeight = 100;
			_rawPlayer.percentWidth = 100;
			
			addChild(_rawPlayer);
		}
		
		override protected function commitProperties():void
		{
			if(_contentChanged)
			{
				_contentChanged = false;
				
				// Adds any child to the _rawPlayer canvas instead of the component root
				for each(var child:UIComponent in content)
				{
					_rawPlayer.addChild(child);
				}
			}
		}
		
		public function set content(value:Array):void
		{
			_content = value;
			_contentChanged = true;
			invalidateProperties();
		}
		
		public function get content():Array
		{
			return _content; 
		}
		
		private function initComponent(e:FlexEvent):void
		{
			// Full screen event listener
			systemManager.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);
			
			// Set default display state & default container for full screen
			_dispState = systemManager.stage.displayState;
			_componentInst = _rawPlayer;
		}
		
		/**
		 *  Toggles fullscreen mode
		 * 
		 */		
		public function fullScreen():void
        {
        	try {
                switch (systemManager.stage.displayState) {
                    case StageDisplayState.FULL_SCREEN:
                        systemManager.stage.displayState = StageDisplayState.NORMAL;
                        break;
                    default:
                        systemManager.stage.displayState = StageDisplayState.FULL_SCREEN;
                        break;
                }
            } catch (e:SecurityError) {
                // Catches any errors
            }
        }
        
        private function fullScreenHandler(e:FullScreenEvent):void
        {
            if (e.fullScreen) {
            	// Reparent the default container to the stage & set its height / width to the stage
                if(_componentInst) if(_componentInst.parent) _componentInst.parent.removeChild(_componentInst);
				systemManager.stage.addChild(_componentInst);
				_originalHeight = _componentInst.height;
				_originalWidth = _componentInst.width;
                _componentInst.width = systemManager.stage.width;
                _componentInst.height = systemManager.stage.height;
            } else {
            	// Reparent the default container back to its parent and reset its size back
                systemManager.stage.removeChild(_componentInst);
                this.addChildAt(_componentInst, 0);
                _componentInst.percentWidth = 100;
                _componentInst.percentHeight = 100;
            }
        }
		
	}
}